<?php
class ymdian_productModel extends model{
	public $base;
	public function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="ymdian_product";
	}
	
	
	public function catlist(){
		return array(
			1=>"吃的",
			 
			2=>"喝的",
			3=>"用的"
		);
	}
	
	public function id_list($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['id']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	public function GetProductIdsBySHop($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['productid']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	public function lsitByShopCat($shopid){
		$option=array(
			"where"=>" shopid=".$shopid." AND status=2 ",
			"order"=>"buy_num DESC"
		);
		
		$data=$this->select($option,$rscount);
		 
		$cats=$ecats=array(
			1=>array(),
			4=>array(),
			2=>array(),
			3=>array()
		);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$v['imgurl']=$products[$v['productid']]['imgurl'];
				$v['price']=$products[$v['productid']]['price'];
				$v['lower_price']=$products[$v['productid']]['lower_price'];
				$v['market_price']=$products[$v['productid']]['market_price'];
				if($v['total_num']<=0){
					$ecats[$v['catid']][]=$v;
				}else{
					$cats[$v['catid']][]=$v;
				}
				
			}
			 
			if(!empty($ecats)){
				
				foreach($ecats as $k=>$v){
					if(!empty($v)){
						foreach($v as $vv){
							$cats[$k][]=$vv;
						}
					}
				}
			}
			
			return $cats;
		}
	}
	
	public function getlistbyshop($shopid){
		$option=array(
			"where"=>" shopid=".$shopid
		);
		$data=$this->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	public function statDayAdd($option){
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kday='".date("Ymd")."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_day")->selectRow($where);
		if($row){
			
			M("stat_product_day")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum']
			),"id=".$row['id']);
		}else{
			M("stat_product_day")->insert($option);
		}		
		
	}
	
	public function statWeekAdd($option){
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kweek='".date("YW")."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_week")->selectRow($where);
		if($row){
			
			M("stat_product_week")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum']
			),"id=".$row['id']);
		}else{
			M("stat_product_week")->insert($option);
		}		
		
	}
	
	public function statMonthAdd($option){
		
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kmonth='".date("Ym")."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_month")->selectRow($where);
		if($row){
			
			M("stat_product_month")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum']
			),"id=".$row['id']);
		}else{
			M("stat_product_month")->insert($option);
		}		
		
	}
	
	/**
	*夜猫店 库存处理
	*
		array(
			"shopid"=>$shopid,
			"productid"=>$productid	,
			"total_num"=>$total_num,
			"content"=>"",
		)
	*/
	public function total_num_add($option){
	 
		$shopid=intval($option['shopid']);
		$productid=intval($option['productid']);
		$total_num=intval($option['total_num']);
		$content=$option['content'];
		$row=M("ymdian_product")->selectRow("shopid=".$shopid." AND productid=".$productid."");
		$product=M("product")->selectRow("id=".$productid);
		if(empty($row)){
			M("ymdian_product")->insert(array(
				"shopid"=>$shopid,
				"productid"=>$productid,
				"total_num"=>$total_num,
				"dateline"=>time(),
				"siteid"=>SITEID
			));
			$row['total_num']=0;
		}else{
			M("ymdian_product")->update(array(
				"total_num"=>$row['total_num']+$total_num
			),"id=".$row['id']);
		}
		//处理记录
		M("ymdian_kclog")->insert(array(
			"shopid"=>$shopid,
			"productid"=>$productid,
			"dateline"=>time(),
			"total_num"=>$total_num,
			"content"=>$content.$product['title']."库存变更".$total_num.",原来".$row['total_num'].",现在".($row['total_num']+$total_num)."",
		));
		
		
	}
	
	
	
	
}

?>